//
//  ViewController.m
//  ChartsDemo
//
//  Created by 孙斌华 on 16/9/8.
//  Copyright © 2016年 Binhua Sun. All rights reserved.
//

#import "ViewController.h"
#import "ChartsDemo-Swift.h"

#import "DayAxisValueFormatter.h"




@interface ViewController ()<ChartViewDelegate>
@property (strong, nonatomic) IBOutlet BarChartView *barChartView;
@property (strong, nonatomic) NSArray *options;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor= [UIColor whiteColor];
    self.title = @"Bar Chart";
    
    self.barChartView.backgroundColor = [UIColor yellowColor];
    [self _setupBarLineChartView:_barChartView];
    
    self.barChartView.delegate = self;

    self.barChartView.drawBarShadowEnabled = NO;//默认YES。显示最上层的值和横线
    self.barChartView.drawValueAboveBarEnabled = YES;
    
    self.barChartView.maxVisibleCount = 60;
 
    
    ChartXAxis *xAxis = self.barChartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    xAxis.labelFont = [UIFont systemFontOfSize:10.f];
    xAxis.drawGridLinesEnabled = NO;
    xAxis.granularity = 1.0; // only intervals of 1 day
    xAxis.labelCount = 7;//个数
//    xAxis.valueFormatter = [[DayAxisValueFormatter alloc] initForChart:self.barChartView];//显示为Day
    
    NSNumberFormatter *leftAxisFormatter = [[NSNumberFormatter alloc] init];
    leftAxisFormatter.minimumFractionDigits = 0;
    leftAxisFormatter.maximumFractionDigits = 1;
    leftAxisFormatter.negativeSuffix = @" $";
    leftAxisFormatter.positiveSuffix = @" $";
    
    ChartYAxis *leftAxis = self.barChartView.leftAxis;
    leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
    leftAxis.labelCount = 8;
    leftAxis.valueFormatter = [[ChartDefaultAxisValueFormatter alloc] initWithFormatter:leftAxisFormatter];
    leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
    leftAxis.spaceTop = 0.15;
    leftAxis.axisMinimum = 0.0; // this replaces startAtZero = YES
    
//    ChartYAxis *rightAxis = self.barChartView.rightAxis;
//    rightAxis.enabled = YES;
//    rightAxis.drawGridLinesEnabled = NO;
//    rightAxis.labelFont = [UIFont systemFontOfSize:10.f];
//    rightAxis.labelCount = 8;
////    rightAxis.valueFormatter = leftAxis.valueFormatter;
//    rightAxis.spaceTop = 0.15;
//    rightAxis.axisMinimum = 0.0; // this replaces startAtZero = YES
    
//    self.barChartView.legend.position = ChartLegendPositionBelowChartRight;
//    self.barChartView.legend.form = ChartLegendFormSquare;
//    self.barChartView.legend.formSize = 9.0;
//    self.barChartView.legend.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
//    self.barChartView.legend.xEntrySpace = 4.0;
    
    
    //选中后，弹出的解释Label
    XYMarkerView *marker = [[XYMarkerView alloc]
                            initWithColor: [UIColor colorWithWhite:180/255. alpha:1.0]
                            font: [UIFont systemFontOfSize:12.0]
                            textColor: UIColor.whiteColor
                            insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0)
                            xAxisValueFormatter: self.barChartView.xAxis.valueFormatter];
    marker.chartView = self.barChartView;
    marker.minimumSize = CGSizeMake(80.f, 40.f);
    self.barChartView.marker = marker;
//
//    _sliderX.value = 12.0;
//    _sliderY.value = 50.0;
    [self setDataCount:5 range:10];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected");
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}


#pragma mark - private

- (void)_setupBarLineChartView:(BarLineChartViewBase *)chartView
{
    chartView.descriptionText = @"描述";//描述
    chartView.noDataTextDescription = @"You need to provide data for the chart.";//没有数据的时候的描述
    
    chartView.drawGridBackgroundEnabled = NO;//背景色
    
    chartView.dragEnabled = YES;//拖拽
    [chartView setScaleEnabled:YES];//手势缩放
    chartView.pinchZoomEnabled = NO;//YES XY同时伸缩，NO XY单独伸缩
    
    // ChartYAxis *leftAxis = chartView.leftAxis;
    
    ChartXAxis *xAxis = chartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;//X轴
    
    chartView.rightAxis.enabled = NO;
}

- (void)setDataCount:(int)count range:(double)range
{
    double start = 0.0;
    
    self.barChartView.xAxis.axisMinimum = start;
    self.barChartView.xAxis.axisMaximum = start + count + 2;
    
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
    
    for (int i = start; i < start + count + 1; i++)
    {
        double mult = (range + 1);
        double val = (double) (arc4random_uniform(mult));
        [yVals addObject:[[BarChartDataEntry alloc] initWithX:(double)i + 1.0 y:val]];
    }
    
    BarChartDataSet *set1 = nil;
//    if (self.barChartView.data.dataSetCount > 0)
//    {
//        set1 = (BarChartDataSet *)self.barChartView.data.dataSets[0];
//        set1.values = yVals;
//        [self.barChartView.data notifyDataChanged];
//        [self.barChartView notifyDataSetChanged];
//    }
//    else
    {
        //设置数据
        set1 = [[BarChartDataSet alloc] initWithValues:yVals label:@""];
        
//        [set1 setColors:ChartColorTemplates.material];//控制柱状图的颜色
        [set1 setColor:[ChartColorTemplates colorFromString:@"#EE4B4B"]];//设置柱状图一种颜色
        
        NSMutableArray *dataSets = [[NSMutableArray alloc] init];
        [dataSets addObject:set1];
        
        BarChartData *data = [[BarChartData alloc] initWithDataSets:dataSets];
        [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];
        
        data.barWidth = 0.9f;
        
        self.barChartView.data = data;
    }
}

@end
